﻿using System;
using System.Drawing;
using System.Windows.Forms;
using GameDesigner.Displayers;
using GameDesigner.Source.Widgets;
using GameLibrary.Model.Core;
using Microsoft.VisualBasic.PowerPacks;
using System.Resources;
using System.Globalization;

namespace GameDesigner.Source.Elements
{
    class ElementDisplayerDT : ElementDisplayer
    {
        public ElementDisplayerDT(GameDisplayer displayer, SimpleShape shape,
                                  ShapeContainer shapeContainer)
            : base(displayer, new GameLocation(shape.Location), shape, shapeContainer)
        {
            this.Shape.MouseMove += new MouseEventHandler(this.Shape_MouseEvent);
            this.Shape.MouseLeave += new EventHandler(this.Shape_MouseLeaveEvent);
            this.Shape.MouseClick += new MouseEventHandler(this.Shape_MouseClick);
            ActiveObject.ObjectChangedEvent += this.OnSelectedElementChanged;
            displayer.GameDef.GameElementDefs.PropertyChangedEvent += this.OnPropertyChanged;
            displayer.GameDef.GamePlayerDefs.PropertyChangedEvent += OnPropertyChangedEvent;
            DisplayIcon();
        }

        private void Shape_MouseEvent(Object target, MouseEventArgs e)
        {
            SimpleShape s = (SimpleShape)target;
            s.BorderColor = Color.Red;
        }

        private void Shape_MouseLeaveEvent(Object target, EventArgs e)
        {
            SimpleShape s = (SimpleShape)target;
            s.BorderColor = Color.Black;
        }

        private void Shape_MouseClick(Object target, EventArgs e)
        {
            if (GameDisplayer.GameDef.GameElementDefs.Contains(Location))
            {
                GameElementDef element = GameDisplayer.GameDef.GameElementDefs[Location];
                ActiveObject.Object = element;
            }
        }

        public override void DisplayIcon()
        {
            GameElementDef e = null;
            if (GameDisplayer.GameDef.GameElementDefs.Contains(Location))
            {
                e = GameDisplayer.GameDef.GameElementDefs[Location];
            }
            if (e == null || e.Icon == "")
            {
                Shape.BackgroundImage = null;
                Shape.BackStyle = BackStyle.Transparent;
                
                if (e.Text.Length > 0)
                {
                    DisplayText(ref e);
                }
            }
            else
            {
                ResourceSet set = global::GameDesigner.Properties.Resources.
                    ResourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, false);

                Bitmap image = (Bitmap)set.GetObject(e.Icon, true);
                Shape.BackgroundImage = image;
                Shape.BackgroundImageLayout = ImageLayout.Center;
                Shape.BackStyle = BackStyle.Opaque;

                if (GameDisplayer.GameDef.GamePlayerDefs.Contains(e.Player))
                {
                    GamePlayerDef player = GameDisplayer.GameDef.GamePlayerDefs[e.Player];
                    Shape.BackColor = player.Color;
                }
            }
        }

        void DisplayText(ref GameElementDef e)
        {
            Bitmap image = new Bitmap(50, 50);
            Graphics g = Graphics.FromImage(image);
            Font font = new Font("Comic Sans MS", 12);
            SolidBrush brush = new SolidBrush(Color.Black);
            Pen pen = new Pen(Color.LightGray, 1);
            g.DrawString(e.Text, font, brush, 0, 0);
            pen.Dispose();
            
            Shape.BackgroundImage = image;
            Shape.BackgroundImageLayout = ImageLayout.Center;
            Shape.BackStyle = BackStyle.Opaque;
        }

        private void OnSelectedElementChanged(object obj)
        {
            GameElementDef element = null;
            if (obj is GameElementDef)
            {
                element = (GameElementDef)obj;
                if (element.Location == base.Location)
                {
                    Shape.FillStyle = FillStyle.Solid;
                    Shape.FillGradientStyle = FillGradientStyle.Horizontal;
                }
                else
                {
                    Shape.FillStyle = FillStyle.Transparent;
                }
            }
            else
            {
                Shape.FillStyle = FillStyle.Transparent;
            }
        }

        void OnPropertyChangedEvent(GamePlayerDef player)
        {
            DisplayIcon();
        }

        private void OnPropertyChanged(GameElementDef element)
        {
            if (element.Location == this.Location)
            {
                DisplayIcon();
            }
        }
    }
}
